package jmine.tec.web.wicket.component;

import java.util.Locale;

import org.apache.wicket.datetime.DateConverter;
import org.apache.wicket.datetime.PatternDateConverter;
import org.apache.wicket.datetime.markup.html.form.DateTextField;
import org.apache.wicket.model.IModel;

import bancosys.tec.utils.date.Date;

/**
 * @author finx
 * @created Aug 2, 2010
 */
public class MapsWicketDateTextField extends DateTextField {
    public static final String DEFAULT_DATE_FORMAT = "dd/MM/yyyy";

    /**
     * Construtor.
     * 
     * @param id id
     * @param model model
     * @param converter converter
     */
    public MapsWicketDateTextField(String id, IModel<Date> model, DateConverter converter) {
        super(id, null, converter);
        this.setType(Date.class);
        this.setDefaultModel(model);
    }

    /**
     * Construtor.
     * 
     * @param id id
     */
    public MapsWicketDateTextField(String id) {
        super(id, null, new MapsWicketPatternDateConverter(DEFAULT_DATE_FORMAT, true));
        this.setType(Date.class);
    }

    /**
     * Construtor.
     * 
     * @param id id
     * @param model model
     */
    public MapsWicketDateTextField(String id, IModel<Date> model) {
        super(id, null, new MapsWicketPatternDateConverter(DEFAULT_DATE_FORMAT, true));
        this.setType(Date.class);
        this.setDefaultModel(model);
    }

    /**
     * Retorna um novo {@link MapsWicketDateTextField}.
     * 
     * @param id o id do componente.
     * @param model o model utilizado no text field.
     * @param required <code>true</code> se for obrigatório, <code>false</code> caso contrário.
     * @param outputMarkupId outputMarkupId
     * @return novo {@link MapsWicketDateTextField}.
     */
    public static MapsWicketDateTextField forDatePattern(String id, IModel<Date> model, boolean required, boolean outputMarkupId) {
        MapsWicketDateTextField field =
                new MapsWicketDateTextField(id, model, new MapsWicketPatternDateConverter(DEFAULT_DATE_FORMAT, true));
        field.setRequired(required);
        field.add(new MapsDatePicker());
        field.setOutputMarkupId(outputMarkupId);
        return field;
    }

    /**
     * Retorna um novo {@link MapsWicketDateTextField}.
     * 
     * @param id o id do componente.
     * @param model o model utilizado no text field.
     * @return novo {@link MapsWicketDateTextField}.
     */
    public static MapsWicketDateTextField forDatePattern(String id, IModel<Date> model) {
        MapsWicketDateTextField field =
                new MapsWicketDateTextField(id, model, new MapsWicketPatternDateConverter(DEFAULT_DATE_FORMAT, true));
        field.add(new MapsDatePicker());
        return field;
    }

    /**
     * Retorna um novo {@link MapsWicketDateTextField}.
     * 
     * @param id o id do componente.
     * @return novo {@link MapsWicketDateTextField}.
     */
    public static MapsWicketDateTextField forDatePattern(String id) {
        MapsWicketDateTextField field = new MapsWicketDateTextField(id);
        field.add(new MapsDatePicker());
        return field;
    }

    /**
     * @author wagner
     */
    public static class MapsWicketPatternDateConverter extends PatternDateConverter {
        /**
         * Construtor.
         * 
         * @param datePattern pattern
         * @param applyTimeZoneDifference applyTimeZoneDifference
         */
        public MapsWicketPatternDateConverter(String datePattern, boolean applyTimeZoneDifference) {
            super(datePattern, applyTimeZoneDifference);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Date convertToObject(String value, Locale locale) {
            java.util.Date date = super.convertToObject(value, locale);
            if (date == null) {
                return null;
            }
            return new Date(date);
        }
    }
}
